//
//  CrosswiseCFL.m
//  MyScrollView
//
//  Created by iOS on 2017/10/26.
//  Copyright © 2017年 iOS. All rights reserved.
//

#import "CrosswiseCFL.h"
#import "CrosswiseCollectionViewCell.h"
#import "CrosswiseFrameModel.h"

@implementation CrosswiseCFL

- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds {
    return YES;
}

- (void)prepareLayout {
    [super prepareLayout];
    CrosswiseFrameModel *frameModel = [CrosswiseFrameModel getCrosswiseFrameModel];
    // 水平滚动
    self.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    // 设置内边距
//    CGFloat inset = (self.collectionView.frame.size.width - self.itemSize.width) * 0.5;
    self.sectionInset = UIEdgeInsetsMake(0, self.collectionView.frame.size.width/2 - frameModel.cellWidth/2, 0, self.collectionView.frame.size.width/2- frameModel.cellWidth/2);
}

- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect {
    
    // 获得super已经计算好的布局属性
    NSArray *array = [super layoutAttributesForElementsInRect:rect];
    //可视区域
    CGRect visiableRect = CGRectMake(self.collectionView.contentOffset.x, self.collectionView.contentOffset.y, self.collectionView.frame.size.width, self.collectionView.frame.size.height);
    CGFloat centetX = self.collectionView.contentOffset.x + self.collectionView.frame.size.width/2;
    //遍历
    for (UICollectionViewLayoutAttributes *attrs in array) {
        //仅设置可视区域
        if (CGRectIntersectsRect(visiableRect, attrs.frame)) {
            // 设置缩放比例
            //cell的中心点x 和CollectionView最中心点的x值
            CGFloat delta = ABS(attrs.center.x - centetX);
            //根据间距值  计算cell的缩放的比例
            //这里scale 必须要 小于1
            CGFloat scale = 1 - delta/self.collectionView.frame.size.width+.4f;
            
            attrs.transform = CGAffineTransformMakeScale(scale, scale);
            //接近中心点的时候
            if (scale > 1.2) {
                CrosswiseCollectionViewCell *cell = (CrosswiseCollectionViewCell *)[self.collectionView cellForItemAtIndexPath:attrs.indexPath];
                [self.collectionView bringSubviewToFront:cell];
            }
        }
    }
    return array;
}

/**
 
 * 这个方法的返回值，就决定了collectionView停止滚动时的偏移量
 
 */

- (CGPoint)targetContentOffsetForProposedContentOffset:(CGPoint)proposedContentOffset withScrollingVelocity:(CGPoint)velocity {
    
    // 计算出最终显示的矩形框
    CGRect rect;
    rect.origin.y = 0;
    rect.origin.x = proposedContentOffset.x;
    rect.size = self.collectionView.frame.size;
    
    // 获得super已经计算好的布局属性
    NSArray *array = [super layoutAttributesForElementsInRect:rect];
    
    // 计算collectionView最中心点的x值
    CGFloat centerX = proposedContentOffset.x + self.collectionView.frame.size.width * 0.5;

    // 存放最小的间距值
    CGFloat minDelta = MAXFLOAT;
    
    for (UICollectionViewLayoutAttributes *attrs in array) {
        if (ABS(minDelta) > ABS(attrs.center.x - centerX)) {
            minDelta = attrs.center.x - centerX;
        }

    }
    // 修改原有的偏移量
    proposedContentOffset.x += minDelta;
    return proposedContentOffset;
}

@end
